﻿// ////////////////////////////////////////////////////////////////////////////
// 
// SWE Case Tool
// 
// Solution:	CaseTool
// Project:		4. MethodCocomo
// Filename:	CtrlCocomoFactor.cs
// 
// File Created:	17:57 31.05.2013 
// Last Changed:	18:16 05.06.2013
// 
// ////////////////////////////////////////////////////////////////////////////

using System;
using System.Windows.Forms;

namespace SWE.CaseTool.Methods.Controls
{
	/// <summary>
	/// Holds one cocomo factor and changes it. It uses the IChanged functionality to noitify the parent
	/// window, if a recalculation is needed.
	/// </summary>
	public partial class CtrlCocomoFactor : UserControl
	{
		/// <summary>
		/// The _factor which is displayed in this control.
		/// </summary>
		private readonly CocomoFactor _factor;

		/// <summary>
		/// Initializes a new instance of the <see cref="CtrlCocomoFactor" /> class.
		/// </summary>
		/// <param name="factor">The factor.</param>
		public CtrlCocomoFactor(CocomoFactor factor)
		{
			_factor = factor;
			InitializeComponent();
		}

		/// <summary>
		/// Handles the Load event of the CtrlCocomoFactor control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void CtrlCocomoFactorLoad(object sender, EventArgs e)
		{
			lblName.Text = _factor.Name;
			numVal.Value = (decimal) _factor.Value;
			numVal.Minimum = (decimal) _factor.Min;
			numVal.Maximum = (decimal) _factor.Max;

			var toolTipContainer = new ToolTip
				{
					IsBalloon = true,
					ToolTipTitle = _factor.Name
				};
			var tooltip = string.Format(@"Kategorie:	{0}
Faktor:		{1}
Minimum:	{2:0.00}
Maximum:	{3:0.00}", _factor.Category, _factor.Name, _factor.Min,
			                            _factor.Max);
			toolTipContainer.SetToolTip(this, tooltip);
			toolTipContainer.SetToolTip(numVal, tooltip);
			toolTipContainer.SetToolTip(lblName, tooltip);
		}

		/// <summary>
		/// Handles the ValueChanged event of the numVal control. Writes the new value back to the factor and
		/// calls if possible the ICHanged event.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">
		/// The <see cref="EventArgs" /> instance containing the event data.
		/// </param>
		private void numVal_ValueChanged(object sender, EventArgs e)
		{
			_factor.Value = (double) numVal.Value;
			if (Parent is IChanged && ((IChanged) Parent).Changed != null)
			{
				((IChanged) Parent).Changed(this, EventArgs.Empty);
			}
		}
	}
}